/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 * Version：V1.0.2(20230701).
 */
package top.hiai.my.setting;

import com.jfinal.aop.Before;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;
import top.hiai.common.interceptor.FrontAuthInterceptor;
import top.hiai.common.model.User;
import top.hiai.common.util.SensitiveWordsKit;
import top.hiai.config.BaseController;
import top.hiai.login.LoginService;

/**
 * @Author www.hiai.top
 * @Email goodsking@163.com
 * @Message 更多资源尽在www.hiai.top
 * @Introduction 类的介绍:个人空间用户资料设置控制器
 */
@Before(FrontAuthInterceptor.class)
public class MySettingController extends BaseController {

    MySettingService mySettingService = new MySettingService();

    /**
     * 渲染个人资料修改视图
     */
    public void index() {
        setAttr("loginUserMessage", mySettingService.getLoginUserMessage(getLoginAccountId()));
        render("profile.html");
    }

    /**
     * 提交验证个人资料的修改
     */
    public void profileSetting() {
        User loginAccount = getAttr(LoginService.loginAccountCacheName);
        String newNickName = getPara("newNickName");
        String newSign = getPara("newSign");
        //敏感词检测
        if (SensitiveWordsKit.checkSensitiveWord(newNickName) != null) {
            renderJson(Ret.fail().set("msg", "昵称中不能包含敏感词。"));
            return;
        }
        if (SensitiveWordsKit.checkSensitiveWord(newSign) != null) {
            renderJson(Ret.fail().set("msg", "个人签名中不能包含敏感词。"));
            return;
        }
        Ret ret = mySettingService.profileSetting(loginAccount.getUid(), newNickName, newSign);
        renderJson(ret);
    }

    /**
     * 渲染安全设置视图
     */
    public void security() {
        render("security.html");
    }

    /**
     * 提交验证密码的修改
     */
    public void securitySetting() {
        User loginAccount = getAttr(LoginService.loginAccountCacheName);
        Ret ret = mySettingService.updatePassword(loginAccount.getUid(), getPara("oldPassword"), getPara("newPassword"), getPara("newPassword_que"));
        renderJson(ret);
    }

    /**
     * 个人认证，这里大家可以改成开VIP等等之类的功能
     */
    public void verify() {
        setAttr("loginUserMessage", mySettingService.getLoginUserMessage(getLoginAccountId()));
        render("verify.html");
    }

    /**
     * 认证验证器，保存认证
     */
    public void verifySetting() {
        User loginAccount = getAttr(LoginService.loginAccountCacheName);
        String authName = getPara("authName");
        //敏感词检测
        if (SensitiveWordsKit.checkSensitiveWord(authName) != null) {
            renderJson(Ret.fail().set("msg", "认证说明中不能包含敏感词。"));
            return;
        }
        Ret ret = mySettingService.verifySetting(loginAccount.getUid(), authName, getPara("trueName"));
        renderJson(ret);
    }

    /**
     * 个人用户的登录历史
     */
    public void loginlog() {
        setAttr("loginLogList", mySettingService.getUserLoginLogPage(getParaToInt("p", 1), limtCount, getLoginAccountId()));
        render("myLoginLog.html");
    }

    /**
     * 删除用户的登录记录
     */
    public void logDelete() {
        mySettingService.deleteLoginLogById(getParaToInt("id"));
        redirect("/my/setting/loginlog");
    }

    /**
     * 用户头像修改
     */
    public void avatar() {
        setAttr("userMsg", mySettingService.getLoginUserMessage(getLoginAccountId()));
        render("myAvatar.html");
    }

    /**
     * 个人中心用户修改用户的头像
     */
    public void uploadAvatar() {
        /** 获取用户的ID */
        int uid = getLoginAccountId();

        UploadFile uf = null;
        try {
            uf = getFile("avatar", mySettingService.getAvatarTempDir(), mySettingService.getAvatarMaxSize());
            if (uf == null) {
                renderJson(Ret.fail().set("msg", "请先选择上传文件"));
                return;
            }
        } catch (Exception e) {
            if (e instanceof com.jfinal.upload.ExceededSizeException) {
                renderJson(Ret.fail().set("msg", "文件大小超出范围"));
            } else {
                if (uf != null) {
                    // 只有出现异常时才能删除，不能在 finally 中删，因为后面需要用到上传文件
                    uf.getFile().delete();
                }
                renderJson(Ret.fail().set("msg", e.getMessage()));
            }
            return;
        }

        Ret ret = mySettingService.uploadAvatar(uid, uf);
        // 上传成功则将文件 url 径暂存起来，供下个环节进行裁切
        if (ret.isOk()) {
            setSessionAttr("avatarUrl", ret.get("avatarUrl"));
        }
        renderJson(ret);
    }

    /**
     * 保存 jcrop 裁切区域为用户头像
     */
    public void saveAvatar() {
        String avatarUrl = getSessionAttr("avatarUrl");
        User user = mySettingService.getLoginUserMessage(getLoginAccountId());
        int x = getParaToInt("x");
        int y = getParaToInt("y");
        int width = getParaToInt("width");
        int height = getParaToInt("height");
        Ret ret = mySettingService.saveAvatar(user, avatarUrl, x, y, width, height);
        renderJson(ret);
    }
}